const router = require('koa-router')();
const cc = require('../conCtl');
const js = require('../jsCtl');
const db = require('../dbCtl');


/**
@hostpath = http://127.0.0.1/service/statistics
 */
 
/** 统计
###
  GET {{hostpath}}
### */
router.get('/', async c => {
    
    let data = {};
    //今日时间
    let today = new Date(new Date().setHours(0, 0, 0, 0)); //获取当天零点的时间
    data.all = await db.start("SELECT (SELECT COUNT(1) FROM t_users) AS userCountAll, ( SELECT sum(money) FROM t_order ) AS saleAll, ( SELECT COUNT(1) FROM t_order WHERE state <> 1 AND state <> 6 ) AS dealUserAll, ( SELECT COUNT(goods_id) FROM t_order WHERE state <> 1 AND state <> 6 ) AS dealGoodsAll, ( SELECT COUNT(1) FROM t_auction_log WHERE offer_type = 2 ) AS offerAll, ( SELECT sum(`view`) FROM t_auction_goods ) AS goodsViewAll, ( SELECT ( SUM(`money`) - SUM(cost_price)) / SUM(cost_price) FROM t_order WHERE state <> 1 AND state <> 6 ) AS profitLvAll, ( SELECT SUM(`money`) - SUM(cost_price) FROM t_order WHERE state <> 1 AND state <> 6 ) AS profitAll, (( SELECT SUM(direction) FROM t_points_log WHERE direction = 1 AND type = 1 ) / ( SELECT SUM(direction) FROM t_points_log WHERE direction = 1 )) AS exchangeScoreRatio");
    data.today = await db.start("SELECT ( SELECT COUNT(1) FROM t_users WHERE dt_in > DATE_FORMAT( CURDATE(), '%Y-%m-%d %H:%i:%s' )) AS userCountAll, ( SELECT sum(money) FROM t_order WHERE dt > DATE_FORMAT( CURDATE(), '%Y-%m-%d %H:%i:%s' )) AS saleAll, ( SELECT COUNT(1) FROM t_order WHERE state <> 1 AND state <> 6 AND dt > DATE_FORMAT( CURDATE(), '%Y-%m-%d %H:%i:%s' )) AS dealUserAll, ( SELECT COUNT(goods_id) FROM t_order WHERE state <> 1 AND state <> 6 AND dt > DATE_FORMAT( CURDATE(), '%Y-%m-%d %H:%i:%s' )) AS dealGoodsAll, ( SELECT COUNT(1) FROM t_auction_log WHERE offer_type = 2 AND dt > DATE_FORMAT( CURDATE(), '%Y-%m-%d %H:%i:%s' )) AS offerAll, ( SELECT sum(`view`) FROM t_auction_goods WHERE dt > DATE_FORMAT( CURDATE(), '%Y-%m-%d %H:%i:%s' )) AS goodsViewAll, ( SELECT ( SUM(`money`) - SUM(cost_price)) / SUM(cost_price) FROM t_order WHERE state <> 1 AND state <> 6 AND dt > DATE_FORMAT( CURDATE(), '%Y-%m-%d %H:%i:%s' )) AS profitLvAll, ( SELECT SUM(`money`) - SUM(cost_price) FROM t_order WHERE state <> 1 AND state <> 6 AND dt > DATE_FORMAT( CURDATE(), '%Y-%m-%d %H:%i:%s' )) AS profitAll, (( SELECT SUM(direction) FROM t_points_log WHERE direction = 1 AND type = 1 AND dt > DATE_FORMAT( CURDATE(), '%Y-%m-%d %H:%i:%s' )) / ( SELECT SUM(direction) FROM t_points_log WHERE direction = 1 AND dt > DATE_FORMAT( CURDATE(), '%Y-%m-%d %H:%i:%s' ))) AS exchangeScoreRatio");
    data.dial = await db.start("SELECT (SELECT COUNT(1) FROM t_gameDial_winner WHERE winnerType = 1) as winCount,(SELECT COUNT(1) FROM t_gameDial_queue WHERE dt > aaa.today) as todayJoinCount,(SELECT SUM(a.price) FROM t_gameDial a INNER JOIN t_gameDial_queue b ON a.id=b.game_id) as scoreAll,(SELECT SUM(a.price) FROM t_gameDial a INNER JOIN t_gameDial_queue b ON a.id=b.game_id WHERE b.dt > aaa.today) as scoreToday FROM (select unix_timestamp(cast(sysdate()as date)) as today) aaa");
    data.lottert = await db.start("SELECT (SELECT COUNT(1) FROM t_gameLottery_winner WHERE winnerType <> 1 AND winnerType <> 3) as winCount,(SELECT COUNT(1) FROM t_gameLottery_ticket WHERE dt > aaa.today) as todayJoinCount,(SELECT SUM(a.price) FROM t_gameLottery a INNER JOIN t_gameLottery_ticket b ON a.id=b.game_id) as scoreAll,(SELECT SUM(a.price) FROM t_gameLottery a INNER JOIN t_gameLottery_ticket b ON a.id=b.game_id WHERE b.dt > aaa.today) as scoreToday FROM(select unix_timestamp(cast(sysdate()as date)) as today) aaa");
    cc.send(c, data);
});

/**
 * 注册用户		总,当日	
	销售额		总,当日	
	成交人数		总,当日	
	成交商品数		总,当日	
	出价次数		总,当日	
	商品点击数		总,当日	
	利润率		总,当日	
	盈利额		总,当日	
	统计现金兑换积分比例		总,当日	用户充值积分/总积分
	拍卖模块销售额		总,当日	(不要)
	抽奖数据统计  大转盘抽奖  中奖参与人数	
                            当日参与人数	
                            总交易额	(消费积分)
                            当日交易额	
                            总利润	(不要)
                            当日利润	(不要)

                将池抽奖	 中奖参与人数	
                            当日参与人数	
                            总交易额	(消费积分)
                            当日交易额	
                            总利润	(不要)
                            当日利润 (不要)
 */

module.exports = router;